package com.zhentao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhentao.model.common.dtos.PageResponseResult;
import com.zhentao.model.common.dtos.ResponseResult;
import com.zhentao.model.common.enums.AppHttpCodeEnum;
import com.zhentao.model.common.wemedia.WmMaterial;
import com.zhentao.model.common.wemedia.WmMaterialDto;
import com.zhentao.service.FileStorageService;
import com.zhentao.service.WmMaterialService;
import com.zhentao.mapper.WmMaterialMapper;
import com.zhentao.utils.wemedia.WmThreadLocalUtil;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;

/**
* @author EWEWW
* @description 针对表【wm_material(自媒体图文素材信息表)】的数据库操作Service实现
* @createDate 2024-06-07 10:48:25
*/
@Service
public class WmMaterialServiceImpl extends ServiceImpl<WmMaterialMapper, WmMaterial>
    implements WmMaterialService{
    @Autowired
    private FileStorageService fileStorageService;
    @Autowired
    private WmMaterialMapper wmMaterialMapper;


    @SneakyThrows
    @Override
    public ResponseResult uploadPicture(MultipartFile multipartFile) {
        //1.检查参数
        if(multipartFile==null || multipartFile.getSize()<=0){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //2.上传图片到minio中
        String path= fileStorageService.uploadImgFile("material",multipartFile.getOriginalFilename(),
                multipartFile.getInputStream() );
        //3.把数据保存到数据库
        WmMaterial wmMaterial=new WmMaterial();
        wmMaterial.setUserId(WmThreadLocalUtil.getUser().getId());
        wmMaterial.setUrl(path);
        wmMaterial.setType((int)0);
        wmMaterial.setIsCollection((int)0);
        wmMaterial.setCreatedTime(new Date());
        wmMaterialMapper.insert(wmMaterial);
        //4.返回结果
        return ResponseResult.okResult(wmMaterial);
    }

    /**
     * 素材列表查询
     * @param dto
     * @return
     */
    @Override
    public ResponseResult findList(WmMaterialDto dto) {
        //1.检查参数
        dto.checkParam();
        //2.分页查询
        Page page = new Page(dto.getPage(), dto.getSize());
        QueryWrapper<WmMaterial> wmMaterialQueryWrapper = new QueryWrapper<>();
        //根据用户id查询
        wmMaterialQueryWrapper.lambda().eq(WmMaterial::getUserId, WmThreadLocalUtil.getUser().getId());
        //按照时间倒叙
        wmMaterialQueryWrapper.orderByDesc("created_time");
        //是否收藏
        wmMaterialQueryWrapper.lambda().eq(dto.getIsCollection() != null, WmMaterial::getIsCollection, dto.getIsCollection());

        page = wmMaterialMapper.selectPage(page, wmMaterialQueryWrapper);

        //3.返回结果
        ResponseResult result=new PageResponseResult(dto.getPage(),dto.getSize(),(int)page.getTotal());
        result.setData(page.getRecords());
        return result;
    }

}




